//
//  JGMainController.swift
//  JGWB
//
//  Created by 付建国 on 2016/10/9.
//  Copyright © 2016年 sher. All rights reserved.
//

import UIKit

class JGMainController: UITabBarController {
    
    //open > public > interal > fileprivate > private swift中的访问控制符

//    fileprivate lazy var composeBtn : UIButton = UIButton.createButton(imageName: "tabbar_compose_icon_add", bgImageName: "tabbar_compose_button")
    
    fileprivate lazy var composeBtn : UIButton = UIButton.init(imageName: "tabbar_compose_icon_add", bgImageName: "tabbar_compose_button")
    
    override func viewDidLoad() {
        super.viewDidLoad()
      /*
        /*
        addChildViewController(childVc: JGHomeController(), title: "首页", imageName: "tabbar_home")
        addChildViewController(childVc: JGHomeController(), title: "消息", imageName: "tabbar_message_center")
        addChildViewController(childVc: JGHomeController(), title: "发现", imageName: "tabbar_discover")
        addChildViewController(childVc: JGHomeController(), title: "我的", imageName: "tabbar_profile")
        */
        
        /*
        addChildViewController(childVcName: "JGHomeController", title: "首页", imageName: "tabbar_home")
        addChildViewController(childVcName: "JGHomeController", title: "消息", imageName: "tabbar_message_center")
        addChildViewController(childVcName: "JGHomeController", title: "发现", imageName: "tabbar_discover")
        addChildViewController(childVcName: "JGHomeController", title: "我的", imageName: "tabbar_profile")
        */
        
        
        //创建好类后 可以根据服务器来控制 APP显示那些页面（机智的想法）
        guard let jsonPath = Bundle.main.path(forResource: "MainVCSettings", ofType: "json") else {
            GOCOOLLog(message: "没有获取到文件路径")
            return
        }
        
        guard let jsonData = NSData.init(contentsOfFile: jsonPath) else{
            return
        }
        
        // 如果调用系统的某些方法时，该方法后面有throws，说明该方法会抛出异常，我们就需要对该异常进行处理
        /*
         在swift中提供三种处理异常的方式
         1.try方式 程序员手动处理异常
         do{
         try JSONSerialization.jsonObject(with: jsonData as Data, options: .mutableContainers)
         }catch{
         print(error)
         }
         2.try?（常用方式） 系统帮助我们处理异常，如果该方法出现了异常，则返回nil，如没有出现异常，则返回该对象（要进行 guard校验）
         3.try!（不推荐使用） 直接告诉系统该方法没有异常，如果出现异常就会Crash
         */
        
        
        guard let anyobject = try? JSONSerialization.jsonObject(with: jsonData as Data, options: .mutableContainers) else {
            return
        }
        
        guard let dictArray = anyobject as? [[String : AnyObject]] else{
            return
        }
        
        for dict in dictArray {
            
            guard let vcName = dict["vcName"] as? String else{
                continue
            }
            
            guard let title = dict["title"] as? String else{
                continue
            }
            
            guard let imageName = dict["imageName"] as? String else{
                continue
            }
            
            addChildViewController(childVcName: vcName, title: title, imageName: imageName)
            
            
        }
        
        
    }
    /*
    //swift支持方法的重载
    //方法的重载：方法名称相同，但是参数不同（参数类型或者参数的个数不同）
    //private在当前文件中可以访问但是其他文件不可以访问
    private func addChildViewController( childVc : UIViewController, title : String, imageName : String) {
    
        childVc.title = title
        childVc.tabBarItem.image = UIImage.init(named: imageName)
        childVc.tabBarItem.selectedImage = UIImage.init(named: "\(imageName)_highlighted")
        let childNav = UINavigationController.init(rootViewController: childVc)
        addChildViewController(childNav)
    }
   */
    
    private func addChildViewController( childVcName : String, title : String, imageName : String) {
        
        //获取命名空间(获取plist文件)
        guard let nameSpace = (Bundle.main.infoDictionary!["CFBundleExecutable"]) as? String else {
            GOCOOLLog(message: "没有获取到命名空间")
            return
        }
        //根据字符串获取对应的class
        guard let ChildVcClass = NSClassFromString(nameSpace + "." + childVcName) else {
            GOCOOLLog(message: "没有获取字符串对应的Class")
            return
        }
        
        guard let childVcTyle = ChildVcClass as? UIViewController.Type else {
            GOCOOLLog(message: "没有获取对应控制器的类型")
            return
        }
        
        let childVc = childVcTyle.init()
        
        childVc.title = title
        childVc.tabBarItem.image = UIImage.init(named: imageName)
        childVc.tabBarItem.selectedImage = UIImage.init(named: "\(imageName)_highlighted")
        let childNav = UINavigationController.init(rootViewController: childVc)
        addChildViewController(childNav)
    }
    */
    
        setupComposeBtn()
    
    }

}

// MARK:- 设置发布按钮
extension JGMainController{
    fileprivate func setupComposeBtn(){
        
        tabBar.addSubview(composeBtn)
        
        composeBtn.center = CGPoint.init(x: tabBar.center.x, y: tabBar.bounds.size.height * 0.5)
        
        // 监听发布按钮的点击
        composeBtn.addTarget(self, action: #selector(composBtnClick(_:)), for: .touchUpInside)
        
    }
}

// MARK:- 时间监听
extension JGMainController{
    // 时间监听本质发送消息，但是发送消息是OC的特性
    // 讲方法包装成@SEL --> 类中查找方法列表 --> 根据@SEL找到imp指针（函数指针） --> 执行函数
    // private不会将该函数添加到方法列表
    // 如果加上@objc 那么该方法依然会被添加到方法列表中
    @objc fileprivate func composBtnClick(_ button : UIButton){
        
        let composeVC = JGComposeController()
        let nav = UINavigationController.init(rootViewController: composeVC)
        
        present(nav, animated: true, completion: nil)
        
    }
}






